.TH std::experimental::optional::value_or 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::experimental::optional::value_or \- std::experimental::optional::value_or

.SH Synopsis
   template< class U >                                (library fundamentals TS)
   constexpr T value_or( U&& default_value ) const&;
   template< class U >                                (library fundamentals TS)
   constexpr T value_or( U&& default_value ) &&;

   Returns the contained value if *this has a value, otherwise returns default_value.

   1) Equivalent to bool(*this) ? **this :
   static_cast<T>(std::forward<U>(default_value)).
   2) Equivalent to bool(*this) ? std::move(**this) :
   static_cast<T>(std::forward<U>(default_value)).

.SH Parameters

   default_value         -         the value to use in case *this is empty
.SH Type requirements
   -
   T must meet the requirements of CopyConstructible in order to use overload \fB(1)\fP.
   -
   T must meet the requirements of MoveConstructible in order to use overload \fB(2)\fP.
   -
   U&& must be convertible to T.

.SH Return value

   The current value if *this has a value, or default_value otherwise.

.SH Exceptions

   Any exception thrown by the selected constructor of the return value T.

.SH Example


// Run this code

 #include <cstdlib>
 #include <experimental/optional>
 #include <iostream>

 std::experimental::optional<const char*> maybe_getenv(const char* n)
 {
     if (const char* x = std::getenv(n))
         return x;
     else
         return {};
 }

 int main()
 {
     std::cout << maybe_getenv("MYPWD").value_or("\fI(none)\fP") << '\\n';
 }

.SH Possible output:

 \fI(none)\fP

.SH See also

   value returns the contained value
         \fI(public member function)\fP

.SH Category:
     * Noindexed pages
